package com.his.struts.action;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.CopyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

import com.his.struts.dao.users.UsersDao;
import com.his.struts.dao.users.UsersImp;
import com.his.struts.form.UserForm;
import com.his.struts.util.AjaxUtil;
import com.his.struts.util.PageUtil;
/**
 * 用户功能类 
 * @author liuyan
 *
 */
public class UserAction extends DispatchAction {

	UsersDao dao = new UsersImp();
	/** 
	 * Method regedit
	 * 用户注册
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward regedit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm users = (UserForm)form;
		ActionForward forword=null;
			//是否注册成功
			boolean ok = dao.regedit(users);
			/*
			 * 如果注册成功 跳登陆页
			 * 否则跳继续注册
			 */
			if(ok){
				forword = mapping.findForward("regeditSuccess");
			}else{
				forword = mapping.findForward("regeditFail");
			}
		return forword;
	}
	
	/** 
	 *
	 * Method userLogin
	 *  登陆
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward userLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		/**
		 * 得到用户form中的名字和密码
		 */
		UserForm userForm = (UserForm) form;
		String name = userForm.getUname();
		String pass = userForm.getUpass();
		HttpSession session = request.getSession(false);
		String myCookie = request.getParameter("cook");
		ActionForward forword = null;
			//判断是否登成功则进行跳转
			boolean ok = dao.login(name, pass);
			if(ok){
				if(myCookie != null){
					String uname = Base64.encode(name, "utf-8");
					String upass = Base64.encode(pass, "utf-8");
					Cookie c = new Cookie("userCookieName",uname);
					Cookie c1 = new Cookie("userCookiePass",upass);
					c.setMaxAge(10000);
					c1.setMaxAge(10000);
					response.addCookie(c);
					response.addCookie(c1);
				}else{
					Cookie[] c = request.getCookies();
					for (Cookie cookie : c) {
						if("userCookieName".equals(cookie.getName())){
							cookie.setValue("");
						}
						if("userCookiePass".equals(cookie.getName())){
							cookie.setValue("");
						}
					}
				}
				request.getSession().setAttribute("loginError", "");
				session.setAttribute("username", name);
				forword = mapping.findForward("loginSuccess");
			} else {
				request.getSession().setAttribute("loginError", "请输入正确的用户名或密码！！！");
				forword = mapping.findForward("loginFail");
			}
		return forword;
	}
	
	
	/** 
	 * Method 取进入首页的数据
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward userIndex(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forword = null;
		HttpSession session = request.getSession();
		String uname = (String)session.getAttribute("username");
		//根据用户名取信息
		UserForm userinfo =  dao.userIndex(uname);
		if(userinfo != null){
			session.setAttribute("userinfo", userinfo);
			forword = mapping.findForward("index");
		}else{
			forword = mapping.findForward("fail");
		}
		return forword;
	}
	
	/** 
	 * Method checkUserName 检查用户是否存在
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward null
	 */
	public ActionForward checkUserName(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm userForm = (UserForm) form; 
		String userName = userForm.getUname();
		/*
		 * 如果用户userName为null 则跳到登陆页
		 */
		if(userName == null){
			return mapping.findForward("login");
		}
		// 检查用户是否存在
		boolean ok = dao.checkUserName(userName);
		/*
		 * ajax发送信息
		 */
		if(ok){
			AjaxUtil.send(response,"该用户名已存在");
		} else {
			AjaxUtil.send(response,"该用户名可以注册");
		}
		return null;
	}
	
	/** 
	 * Method allUser 所有用户
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward allUser
	 */
	public ActionForward allUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		 
		String sql = "userinfo order by uid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		// 如果数据不为空 
		if(rs != null){
			// 封装
			List<UserForm> allUser = dao.getUserBean(rs);
			// 数据放到request中
			request.setAttribute("allUser", allUser);
		}
		 
		return mapping.findForward("allUser");
	}
	/** 
	 *取出Cookie
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward allUser
	 */
	public ActionForward getUserCookie(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Cookie[] c = request.getCookies();
		for (Cookie cookie : c) {
			if("userCookieName".equals(cookie.getName())){
				String uname = cookie.getValue();
				String name = Base64.decode(uname, "utf-8");
				request.getSession().setAttribute("cookiename", name);
			}
			if("userCookiePass".equals(cookie.getName())){
				String upass = cookie.getValue();
				String pass = Base64.decode(upass, "utf-8");
				request.getSession().setAttribute("cookiepass", pass);
			}
			if("docCookieName".equals(cookie.getName())){
				String uname = cookie.getValue();
				String name = Base64.decode(uname, "utf-8");
				request.getSession().setAttribute("doccookiename", name);
			}
			if("docCookiePass".equals(cookie.getName())){
				String upass = cookie.getValue();
				String pass = Base64.decode(upass, "utf-8");
				request.getSession().setAttribute("doccookiepass", pass);
			}
		}
		return null;
	}
	/** 
	 * Method deleteUser 删除用户
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward allUser
	 */
	public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm userForm = (UserForm) form; 
		
		int uid = userForm.getUid();
		/*
		 * 如果用户id为0 则跳到登陆页
		 */
		if(uid == 0){
			return mapping.findForward("login");
		}
		// 根据id 删除用户
		dao.deleteUser(uid);
		
		String sql = "userinfo order by uid desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		// 如果数据不为空 
		if(rs != null){
			// 封装
			List<UserForm> allUser = dao.getUserBean(rs);
			// 数据放到request中
			request.setAttribute("allUser", allUser);
		}
		 
		request.setAttribute("message", "删除成功");
		return mapping.findForward("allUser");
	}
	
	/** 
	 * Method updateInfo 更新用户资料
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 用户资料页
	 */
	public ActionForward updateInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm userForm = (UserForm) form; 
	
		// 得到用户名
		String userName = userForm.getUname();
		// 得到文件
		FormFile file = userForm.getPic();
		if(file != null){
			// 项目绝对路径
			String path = request.getRealPath("images/touxiang/user");
			// 得到写出途径 
			String outPath = path+"\\"+userForm.getUpicture();
			// 创建写出流
			OutputStream output = new FileOutputStream(outPath);
			// 创建写入流
			InputStream input = file.getInputStream();
			// 把写入流里的文件 copy 到写出流中 且写入
			CopyUtils.copy(input, output);
			input.close();
			output.close();
		} else {
			request.getSession().setAttribute("message", "不支持此格式");
			return mapping.findForward("userInfo");
		}
		// 把用户修改到信息更新到数据库
		dao.updateUserInfo(userForm);
		// 重新读取用户信息
		UserForm userinfo =  dao.userIndex(userName);
		// 把用户最新信息更新到session中
		request.getSession().setAttribute("userinfo", userinfo);
		
		request.getSession().setAttribute("message", "更新成功，请刷新查看头像");
		return mapping.findForward("userInfo");
	}
	
}